package controller;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.UserDTO;
import DAO.UserDAO;
import DAO.DAOFactory;
import DAO.NotFoundException;

/**
 * Servlet implementation class LoginController
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;


	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		HttpSession userSession = request.getSession(false);
		if (userSession == null) {
			System.out.println("user not logged in");
			RequestDispatcher rd = request.getRequestDispatcher("/Login.jsp");
			rd.forward(request, response);
		}
		else
			doProcess(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);			
	}

	private void doProcess(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String address = null;		
		String id = request.getParameter("userId").trim();
		String p = request.getParameter("password").trim();
		int UID=0;		
		//address = "/Test.jsp";
		System.out.println("UserID: "+id+"Password: "+p);

		if (id==null || id.equals("") || p==null || p.equals("")) {
			System.out.println("null, no value entered");			
			address = "/Login.jsp";
			request.setAttribute("error2", "*Please verify your UserID and Password.");
			RequestDispatcher rd = request.getRequestDispatcher(address);		   	
			try {
				rd.forward(request, response);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			// try to convert to integer
			try {
				UID = Integer.parseInt(id);
			} catch (NumberFormatException e) {
				address = "/Login.jsp";
				request.setAttribute("error1", "*UserID can only be numbers.");
//				RequestDispatcher rd = request.getRequestDispatcher(address);
//				try {
//					//rd.forward(request, response);
//				} catch (ServletException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
			}

			// try to check user credential
			UserDAO u = DAOFactory.loadInstance().getUserDAO();
			UserDTO user = new UserDTO();
			try {
				user = u.findbyId(UID);
				System.out.println("User: "+user);
				if (user.getPassword()==null) {
					address = "/Login.jsp";
					request.setAttribute("error3", "*Invalid Login. UserID and Password don't match.");
					RequestDispatcher rd = request.getRequestDispatcher(address);				   	
					try {
						rd.forward(request, response);
					} catch (ServletException e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					}
				}			
				else { 
					System.out.println(user.getPassword()+": "+p);
					if (user.getPassword().equals(p)) {
						
						//to create a session for valid user
//						HttpSession userSession = request.getSession(true);
						request.getSession().setAttribute("LoggedinUser", user);

						//to check user role
						System.out.println("role");
						HttpSession roleSession = request.getSession(true);
						roleSession.setAttribute("roleUser", user.getRole());
//						if (user.getRole().equals("Admin")) {
//							response.sendRedirect("/BookingHistory.jsp");
//							return;}
//
//						if (user.getRole().equals("Manager")) {
//							response.sendRedirect("ReviewController");
//							return;}
//						if (user.getRole().equals("Staff")) {
//							response.sendRedirect("StaffMenu.jsp");	
//							return;}	
						
						response.sendRedirect("menu.jsp");
						//return;
					}
					else {
						address = "/Login.jsp";
						request.setAttribute("error4", "*Invalid Login. Incorrect Password.");
						RequestDispatcher rd = request.getRequestDispatcher(address);					   	
						try {
							rd.forward(request, response);
						} catch (ServletException e3) {
							// TODO Auto-generated catch block
							e3.printStackTrace();
						}

					}
				
				}
			} catch (NotFoundException e1) {
				System.out.println("found");
				e1.printStackTrace();
				} 
		}
		

		//to display user name
		//		HttpSession userSession = null;
		//		UserDTO user = new UserDTO();
		//		String name = (String)userSession.getAttribute(user.getUsername());
		//		if (name != null)
		//			System.out.println("Welcome: " + name);
		//			//address = "/Test.jsp";

	}	
}

